package org.dromara.Nyt7PremiumSubsidyInfo.service.impl;

import org.dromara.common.core.utils.MapstructUtils;
import org.dromara.common.core.utils.StringUtils;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.common.mybatis.core.page.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.dromara.Nyt7PremiumSubsidyInfo.domain.bo.Nyt7PremiumSubsidyInfoBo;
import org.dromara.Nyt7PremiumSubsidyInfo.domain.vo.Nyt7PremiumSubsidyInfoVo;
import org.dromara.Nyt7PremiumSubsidyInfo.domain.Nyt7PremiumSubsidyInfo;
import org.dromara.Nyt7PremiumSubsidyInfo.mapper.Nyt7PremiumSubsidyInfoMapper;
import org.dromara.Nyt7PremiumSubsidyInfo.service.INyt7PremiumSubsidyInfoService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 保费补贴比例Service业务层处理
 *
 * @author Lion Li
 * @date 2024-12-31
 */
@RequiredArgsConstructor
@Service
public class Nyt7PremiumSubsidyInfoServiceImpl implements INyt7PremiumSubsidyInfoService {

    private final Nyt7PremiumSubsidyInfoMapper baseMapper;

    /**
     * 查询保费补贴比例
     *
     * @param id 主键
     * @return 保费补贴比例
     */
    @Override
    public Nyt7PremiumSubsidyInfoVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 分页查询保费补贴比例列表
     *
     * @param bo        查询条件
     * @param pageQuery 分页参数
     * @return 保费补贴比例分页列表
     */
    @Override
    public TableDataInfo<Nyt7PremiumSubsidyInfoVo> queryPageList(Nyt7PremiumSubsidyInfoBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<Nyt7PremiumSubsidyInfo> lqw = buildQueryWrapper(bo);
        Page<Nyt7PremiumSubsidyInfoVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询符合条件的保费补贴比例列表
     *
     * @param bo 查询条件
     * @return 保费补贴比例列表
     */
    @Override
    public List<Nyt7PremiumSubsidyInfoVo> queryList(Nyt7PremiumSubsidyInfoBo bo) {
        LambdaQueryWrapper<Nyt7PremiumSubsidyInfo> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<Nyt7PremiumSubsidyInfo> buildQueryWrapper(Nyt7PremiumSubsidyInfoBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<Nyt7PremiumSubsidyInfo> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getCentralFinanceSubsidy() != null, Nyt7PremiumSubsidyInfo::getCentralFinanceSubsidy, bo.getCentralFinanceSubsidy());
        lqw.eq(bo.getProvincialFinanceSubsidy() != null, Nyt7PremiumSubsidyInfo::getProvincialFinanceSubsidy, bo.getProvincialFinanceSubsidy());
        lqw.eq(bo.getCityFinanceSubsidy() != null, Nyt7PremiumSubsidyInfo::getCityFinanceSubsidy, bo.getCityFinanceSubsidy());
        lqw.eq(bo.getDistrictFinanceSubsidy() != null, Nyt7PremiumSubsidyInfo::getDistrictFinanceSubsidy, bo.getDistrictFinanceSubsidy());
        lqw.eq(bo.getOtherFinanceSubsidy() != null, Nyt7PremiumSubsidyInfo::getOtherFinanceSubsidy, bo.getOtherFinanceSubsidy());
        return lqw;
    }

    /**
     * 新增保费补贴比例
     *
     * @param bo 保费补贴比例
     * @return 是否新增成功
     */
    @Override
    public Boolean insertByBo(Nyt7PremiumSubsidyInfoBo bo) {
        Nyt7PremiumSubsidyInfo add = MapstructUtils.convert(bo, Nyt7PremiumSubsidyInfo.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改保费补贴比例
     *
     * @param bo 保费补贴比例
     * @return 是否修改成功
     */
    @Override
    public Boolean updateByBo(Nyt7PremiumSubsidyInfoBo bo) {
        Nyt7PremiumSubsidyInfo update = MapstructUtils.convert(bo, Nyt7PremiumSubsidyInfo.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(Nyt7PremiumSubsidyInfo entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 校验并批量删除保费补贴比例信息
     *
     * @param ids     待删除的主键集合
     * @param isValid 是否进行有效性校验
     * @return 是否删除成功
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteByIds(ids) > 0;
    }



    /**
     * 根据ID列表查询保费补贴比例列表
     *
     * @param ids ID列表
     * @return 保费补贴比例列表
     */
    @Override
    public List<Nyt7PremiumSubsidyInfoVo> queryListByIds(List<Long> ids) {
        LambdaQueryWrapper<Nyt7PremiumSubsidyInfo> lqw = Wrappers.lambdaQuery();
        lqw.in(Nyt7PremiumSubsidyInfo::getId, ids);
        return baseMapper.selectVoList(lqw);
    }




}
